import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:common/util/t_util.dart';

class TScannerListener extends StatelessWidget {
  final Widget child;
  final Function onNewString;
  final FocusNode _focusNode = FocusNode();
  Map<int,StringBuffer> _map = <int,StringBuffer>{};
  TScannerListener({@required this.child, @required this.onNewString});
  String data;
  @override
  Widget build(BuildContext context) {
    FocusScope.of(context).requestFocus(_focusNode);
    return RawKeyboardListener(
      child: child,
      onKey: (event) {
        //print("扫描过程数据  keyLabel = ${event.data.physicalKey}  keyId = ${event.data}  isShiftPressed = ${event.data.isShiftPressed}  deviceId = ");
        if(event.data is RawKeyEventDataAndroid) {
          RawKeyEventDataAndroid dataAndroid = event.data;
          if (TUtil.isEmptyString(event.character)) {
            if (TUtil.isNotEmptyString(event.data.logicalKey.keyLabel)) {
              if(_map[dataAndroid.deviceId] == null){
                StringBuffer sb = StringBuffer("");
                sb.write(_keyCodeToChar(dataAndroid.keyCode, dataAndroid.isShiftPressed));
                _map[dataAndroid.deviceId] = sb;
              }else{
                _map[dataAndroid.deviceId].write(_keyCodeToChar(dataAndroid.keyCode, dataAndroid.isShiftPressed));
              }
              data = _map[dataAndroid.deviceId].toString();
              if(data.length > 200 || (data.startsWith("orderId=") && data.contains("&performOrderId=") && data.length > 70)){
                print('扫描结果数据(异常)：$data deviceId= ${dataAndroid.deviceId}, productId = ${dataAndroid.productId}  vendorId = ${dataAndroid.vendorId}');
                _map[dataAndroid.deviceId] = null;
                onNewString(data, true);
              }
            } else {
              if (event.data.logicalKey.keyId == 4295426088 && _map[dataAndroid.deviceId].isNotEmpty) {
                final finalString = _map[dataAndroid.deviceId].toString();
                _map[dataAndroid.deviceId] = null;
                print('扫描结果数据：$finalString deviceId= ${dataAndroid.deviceId}, productId = ${dataAndroid.productId}  vendorId = ${dataAndroid.vendorId}');
                onNewString(finalString,false);
              }
            }
          }
        }
        /*  int code = event.character != null && event.character.length > 0
            ? event.character.codeUnitAt(0)
            : -1;
        if (code != -1) {
          if (code != 10) {
            _sb.write(event.character);
          } else {
            final finalString = _sb.toString();
            _sb.clear();
            onNewString(finalString);
          }
        }*/
      },
      autofocus: true,
      focusNode: _focusNode,
    );
  }

  String _keyCodeToChar(int code, bool isShift) {
    switch (code) {
      case 59:
      case 60:
      case 62:
      case 20:
      case 19:
        return "";
      case 7:
        return isShift ? ")" : "0";
      case 8:
        return isShift ? "!" : "1";
      case 9:
        return isShift ? "@" : "2";
      case 10:
        return isShift ? "#" : "3";
      case 11:
        return isShift ? '\$' : "4";
      case 12:
        return isShift ? "%" : "5";
      case 13:
        return isShift ? "^" : "6";
      case 14:
        return isShift ? "&" : "7";
      case 15:
        return isShift ? "*" : "8";
      case 16:
        return isShift ? "(" : "9";

      case 29:
        return isShift ? "A" : "a";
      case 30:
        return isShift ? "B" : "b";
      case 31:
        return isShift ? "C" : "c";
      case 32:
        return isShift ? "D" : "d";
      case 33:
        return isShift ? "E" : "e";
      case 34:
        return isShift ? "F" : "f";
      case 35:
        return isShift ? "G" : "g";
      case 36:
        return isShift ? "H" : "h";
      case 37:
        return isShift ? "I" : "i";
      case 38:
        return isShift ? "J" : "j";
      case 39:
        return isShift ? "K" : "k";
      case 40:
        return isShift ? "L" : "l";
      case 41:
        return isShift ? "M" : "m";
      case 42:
        return isShift ? "N" : "n";
      case 43:
        return isShift ? "O" : "o";
      case 44:
        return isShift ? "P" : "p";
      case 45:
        return isShift ? "Q" : "q";
      case 46:
        return isShift ? "R" : "r";
      case 47:
        return isShift ? "S" : "s";
      case 48:
        return isShift ? "T" : "t";
      case 49:
        return isShift ? "U" : "u";
      case 50:
        return isShift ? "V" : "v";
      case 51:
        return isShift ? "W" : "w";
      case 52:
        return isShift ? "X" : "x";
      case 53:
        return isShift ? "Y" : "y";
      case 54:
        return isShift ? "Z" : "z";

      case 55:
        return isShift ? "<" : ",";
      case 56:
        return isShift ? ">" : ".";
      case 76:
        return isShift ? "?" : "/";
      case 73:
        return isShift ? "|" : "\\";
      case 75:
        return isShift ? "\"" : "'";
      case 74:
        return isShift ? ":" : ";";
      case 71:
        return isShift ? "{" : "[";
      case 72:
        return isShift ? "}" : "]";
      case 68:
        return isShift ? "~" : "`";
      case 70:
        return isShift ? "+" : "=";
      case 69:
        return isShift ? "_" : "-";
      case 156:
        return "-";
      case 154:
        return "/";
      case 155:
        return "*";
      case 158:
        return ".";
      case 157:
        return "+";
      case 159:
        return ",";
      case 161:
        return "=";
      case 162:
        return "(";
      case 163:
        return ")";
      default:
        return "?";
    }
  }

}
